﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace ResortManager
{
    public class Util
    {
        private static RegistryKey m_regResortMngr;
        public static RegistryKey ResortManagerRegKey
        {
            get
            {
                if (m_regResortMngr == null)
                {
                    m_regResortMngr = Registry.CurrentUser.CreateSubKey("ResortManager");
                }
                return m_regResortMngr;
            }
        }

        public static void CleanUp()
        {
            m_regResortMngr.Close();
            m_regResortMngr = null;
        }

        public static void AddUserList(string sUserName)
        {
            string sUserList = (string)ResortManagerRegKey.GetValue("UserList", string.Empty);
            if (!sUserList.Contains(sUserName))
            {
                sUserList += sUserName + ";";
                ResortManagerRegKey.SetValue("UserList", sUserList);
            }
        }

        public static IList<string> GetUserList()
        {
            string sUserList = (string)ResortManagerRegKey.GetValue("UserList", string.Empty);
            string[] arr = sUserList.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); 
            return arr ?? new string[0];
        }

        public static void AddRememberInfo(string sUser, string sPass)
        {
            ResortManagerRegKey.SetValue("RememberAccount", sUser);
            ResortManagerRegKey.SetValue("RememberPass", sPass);
        }

        public static bool GetRememberInfo(out string sUser, out string sPass)
        {
            sUser = (string)ResortManagerRegKey.GetValue("RememberAccount", string.Empty);
            sPass = (string)ResortManagerRegKey.GetValue("RememberPass", string.Empty);
            return !(string.IsNullOrEmpty(sUser) || string.IsNullOrEmpty(sPass));
        }

        public static void AddAdvancedConfig(string sServer, string sDBName, string sDBAcc, string sDBPass)
        {
            string sAdvancedInfo = string.Format("{0}{4}{1}{4}{2}{4}{3}", sServer, sDBName, sDBAcc, sDBPass, (char)32000);
            ResortManagerRegKey.SetValue("AdvancedConfig", sAdvancedInfo, RegistryValueKind.String);
        }

        public static bool GetAdvancedConfig(out string sServer, out string sDBName, out string sDBAcc, out string sDBPass)
        {
            sServer = sDBName = sDBAcc = sDBPass = string.Empty;
            string sInfo = (string)ResortManagerRegKey.GetValue("AdvancedConfig", string.Empty);
            if (!string.IsNullOrEmpty(sInfo))
            {
                string[] arr = sInfo.Split(new char[] { (char)32000 }, StringSplitOptions.RemoveEmptyEntries);
                if (arr.Length == 4)
                {
                    sServer = arr[0];
                    sDBName = arr[1];
                    sDBAcc = arr[2];
                    sDBPass = arr[3];
                }
            }

            return !(string.IsNullOrEmpty(sServer) || string.IsNullOrEmpty(sDBName)
                || string.IsNullOrEmpty(sDBAcc) || string.IsNullOrEmpty(sDBPass));
        }
    }
}
